﻿$().ajaxStart(function() {
    var div = $("#divCategory");
    var blockMsg = "<div class=\"loadingMsg\"><img src=\"/image/ajax-loader.gif\" />请稍候...</div>";

    div.block({ message: blockMsg });
}).ajaxComplete(function() {
    var div = $("#divCategory");
    div.unblock();
    BindEvents();
});

$(document).ready(BindEvents);

function BindEvents() {
    var div = $("#divCategory");
    var table = div.find("table");
    table.find("tr").each(function() {
        var id = $(this).find("td:eq(0)>:hidden").val();
        var addButton = $(this).find("td:eq(1)>:button:eq(0)");
        var editButton = $(this).find("td:eq(1)>:button:eq(1)");
        var deleteButton = $(this).find("td:eq(1)>:button:eq(2)");
        var setButton = $(this).find("td:eq(2)>:button");

        addButton.unbind("click").bind("click", function() {
            location.href = "add.aspx?pid=" + id;
        });

        editButton.unbind("click").bind("click", function() {
            location.href = "edit.aspx?id=" + id;
        });

        deleteButton.unbind("click").bind("click", function() {
            jConfirm("确定要删除当前项？", "警告！", function(isOK) {
                if (isOK) {
                    $.get("index.aspx", { action: "delete", id: id }, function(data) {
                        switch (data) {
                            case null:
                                jAlert("服务器错误，请稍候重试", "提示");
                                break;
                            case "-1":
                                jAlert("当前类别下有子类别，不允许删除", "提示");
                                break;
                            case "-2":
                                jAlert("根类别下的最后一个类别允许删除", "提示");
                                break;
                            case "0":
                                jAlert("操作失败", "提示");
                            case null:
                                jAlert("服务器错误，请稍候重试", "提示");
                                break;
                            default:
                                $("#divCategory").fadeOut("slow").html(data).fadeIn("slow");
                                jAlert("操作成功", "提示");
                                break;
                        }
                    });
                }
            })
        });

        setButton.unbind("click").bind("click", function() {
            var sortNumber = $(this).prev().val();

            $.get("index.aspx", { action: "setNumber", id: id, number: sortNumber }, function(data) {
                switch (data) {
                    case "-1":
                        jAlert("类别顺序必须在当前最大值和最小值之间", "提示");
                        return;
                    case "0":
                        jAlert("操作失败", "提示");
                        return;
                    case null:
                        jAlert("服务器错误，请稍候重试", "提示");
                        return;
                    default:
                        $("#divCategory").fadeOut("slow").html(data).fadeIn("slow");
                        jAlert("操作成功", "提示");
                        return;
                }
            });
        });
    });
}